﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Net;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Xml;

namespace DataAccess
{
    public partial class DataAccess
    {
        public List<Entities.Storage_Account.StorageAccount> GetStorageAccountList(String SunscriptionID, String CertificatePath)
        {
            List<Entities.Storage_Account.StorageAccount> List = new List<Entities.Storage_Account.StorageAccount>();

            string requestUrl = "https://management.core.windows.net/" + SunscriptionID + "/services/storageservices";

            string ReturnBody = string.Empty;


            WebResponse resp = null;
            try
            {
                NameValueCollection RequestHeaders = new NameValueCollection();
                RequestHeaders.Add("x-ms-version", x_ms_version);

                X509Certificate cert = X509Certificate.CreateFromCertFile(CertificatePath);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                request.Method = "GET";
                request.ClientCertificates.Add(cert);
                if (RequestHeaders != null)
                {
                    request.Headers.Add(RequestHeaders);
                }

                resp = request.GetResponse();

                if (resp != null)
                {

                    using (StreamReader sr = new StreamReader(resp.GetResponseStream(), true))
                    {
                        ReturnBody = sr.ReadToEnd();
                        sr.Close();
                    }
                    if (ReturnBody != null)
                    {
                        XmlDocument xdoc = new XmlDocument();
                        xdoc.LoadXml(ReturnBody.ToString());

                        XmlNodeList Urlname = xdoc.GetElementsByTagName("Url");
                        XmlNodeList ServiceName = xdoc.GetElementsByTagName("ServiceName");
                        StringBuilder sb = new StringBuilder();

                        for (int i = 0; i < Urlname.Count; i++)
                        {

                            Entities.Storage_Account.StorageAccount StorageAccount =new Entities.Storage_Account.StorageAccount()
                            {
                                ServiceName = ServiceName[i].InnerText,
                                Url = Urlname[i].InnerText
                            };


                            List.Add(StorageAccount);

                        }

                    }


                }
            }
            
            finally
            {

                if (resp != null) { resp.Close(); }
                resp = null;
            }
          


            return List;
        }

    }
}
